package com.example.sys.ai.service;

import com.example.sys.ai.dto.BankCard;
import com.example.sys.ai.dto.BusinessLicense;
import com.example.sys.ai.dto.IDCard;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class SysAiServiceImpl implements SysAiService {

    Logger logger = LoggerFactory.getLogger(getClass());

    private SysAiHandler sysAiHandler;

    @Value("${ai.service.class}")
    private String aiServiceClass;

    @Override
    public SysAiHandler getSysAiHandler() {
        if (this.sysAiHandler == null) {
            try {
                Class<?> clazz = Class.forName(this.aiServiceClass);
                this.sysAiHandler = (SysAiHandler) clazz.newInstance();
            } catch (Exception e) {
                logger.warn("加载ai api接口实现失败: " + e.getMessage());
            }
        }
        return this.sysAiHandler;
    }


    /***
     * 身份证识别，正面照片面
     * @param imgData
     * @return
     * @throws Exception
     */
    @Override
    public IDCard.Front idCardFront(byte[] imgData) throws Exception {
        return getSysAiHandler().idCardFront(imgData);
    }

    /**
     * 身份证识别，反面国徽面
     *
     * @param imgData
     * @return
     * @throws Exception
     */
    @Override
    public IDCard.Back idCardBack(byte[] imgData) throws Exception {
        return getSysAiHandler().idCardBack(imgData);
    }

    /***
     * 银行卡识别
     * @param imgData
     * @return
     * @throws Exception
     */
    @Override
    public BankCard bankCard(byte[] imgData) throws Exception {
        return getSysAiHandler().bankCard(imgData);
    }

    /***
     * 营业执照识别
     * @param imgData
     * @return
     * @throws Exception
     */
    @Override
    public BusinessLicense businessLicense(byte[] imgData) throws Exception {
        return getSysAiHandler().businessLicense(imgData);
    }
}
